<?php
class advController extends RPHP_Controller
{

    //权限前缀
    protected $_pvprefix = "adv";
/*----文章--------------------*/
    //文章列表
    public function advlist()
    {

        $priv = array($this->_pvprefix . "_al");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;

            if ($_GET['keyword'] != "") {
                $keyword = addslashes(trim($_GET['keyword']));
                if ($keyword != '') {
                    $para['keyword'] = $keyword;
                }

            }

            if (intval($_GET['page']) > 0) {
                $page = intval($_GET['page']);
            } else {
                $page = 1;
            }
            

            if (isset($_GET['position']) && $_GET['position'] != "") {
                $order_status = intval($_GET['position']);
                if ($order_status > 0) {
                    $para['position'] = $order_status;
                }
            
            }
            
            if ($_GET['caid'] != "") {
                $caid = intval($_GET['caid']);
                if ($caid > 0) {
                    $para['caid'] = $caid;
                }
            
            }
            
            if (isset($_GET['status']) && $_GET['status'] != "") {
               
                    $para['status'] = intval($_GET['status']);
            
            }
           // print_r($para);die;
            $res              = $model->getArticles($para, $page);
            $this->view->data = $res;
            $this->view->cate = $model->getGoodsCategory();
            //获取页码
            if ($this->view->data['maxpage'] > 1) {
                $this->view->data['spage'] = $model->fun->spage($this->view->data['page'], $this->view->data['maxpage']);
            }

            //新增权限
            $this->view->pv['add'] = $model->checkPriv($this->_pvprefix . "_vad", false);
            //编辑权限
            $this->view->pv['edit'] = $model->checkPriv($this->_pvprefix . "_ved", false);

            //删除权限
            $this->view->pv['del'] = $model->checkPriv($this->_pvprefix . "_vde", false);

            $this->display();

        }

    }

    //新增文章
    public function adv_add()
    {

        $priv = array($this->_pvprefix . "_vad");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;
            $this->view->cate = $model->getPcategory();
            //$this->view->cate         = $model->getCategory();
            $this->view->uploadImages = array(
                array('name' => '封面图'),

            );
            $this->display();
        }

    }

    //编辑文章
    public function adv_edit()
    {
        $priv = array($this->_pvprefix . "_ved");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $id = intval($_GET['id']);
           
            $status = $_GET['status'];
            $keyword = trim($_GET['keyword']);
            $position = intval($_GET['position']);
            $caid = intval($_GET['caid']);
            $id = intval($_GET['id']);
            if ($id > 0) {
                $data = $model->getSingleArticle($id);
            }
            $where= '';
            if ($data) {
                if($status || $keyword || $position || $caid){
                    $where = "?status=".$status."&keyword=".$keyword."&position=".$position."&caid=".$caid;
                    $this->view->wheres = $where;
                }
                $this->view->auth = $auth;
                $this->view->data = $data;

                if ($data['pic'] != '') {
                    $img_url = $model->getDocs($data['pic']);
                    $this->view->img = $img_url;
                    
                }

              
                $this->view->cate = $model->getPcategory();
               
                $this->display();

            } else {
                RPHP_RR::nodata();
            }

        }

    }

    //分类排序
    public function advsort()
    {
    
        $priv = array($this->_pvprefix . "_ved");
    
        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;
            $this->view->data = $model->getAdvLists(intval($_GET['position']), true, 0);
            $this->display();
    
        }
    
    }
    public function ajax()
    {

        $priv         = array($this->_pvprefix . "_al");
        $model        = $this->model;
        $auth         = $model->checkPriv($priv, true, false);
        $data['code'] = 901;
        if ($auth) {
            $data['code'] = 900;
            if ($_POST['act'] == "getattr") {

                $data = $model->getFormAttr($_POST['typeid']);

            } elseif ($_POST['act'] == "remove") {
                $id   = $_POST['id'];
                $data = $model->removeArticle($id);

            }

        }

        echo json_encode($data);

    }

    public function savearticle()
    {
        $priv = array($this->_pvprefix . "_al");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;
           
            //set_time_limit(0);

            if ($_POST['act'] == "add" && $model->checkPriv($this->_pvprefix . "_vad", false)) {
                $this->view->success = "新增广告成功";
                $this->view->link    = array(
                    array('name' => '继续新增广告', 'url' => '/adv/adv_add'),
                    array('name' => '返回广告列表', 'url' => '/adv/advlist'),
                );
               
               $this->view->code = $model->saveArticle($_POST, $_FILES);
            } elseif ($_POST['act'] == "edit" && $model->checkPriv($this->_pvprefix . "_ved", false)) {
                    if($_POST['where']){
                        $where = $_POST['where'].'&id='.$_POST['id'];
                        
                    }else{
                        
                        $where = '?id='.$_POST['id'];
                    }
                $this->view->success = "编辑广告成功";
                $this->view->link    = array(
                     array('name' => '重新编辑', 'url'=>'/adv/adv_edit'.$where),
                    array('name' => '返回广告列表', 'url' => '/adv/advlist'.$_POST['where']),
                );
               $this->view->code = $model->saveArticle($_POST, $_FILES, $_POST['id']);
                
            }elseif ($_POST['act'] == "sort" && $model->checkPriv($this->_pvprefix . "_ved", false)) {

                $this->view->success = "保存分类排序成功";
                $this->view->link    = array(
                    array('name' => '返回广告列表', 'url' => '/adv/advlist?position='.$_POST['position']),
                );
                $data = $model->saveAdvSort($_POST);
               
            }
            //$this->view->code = $data;
            $this->displaydo();

        }
    }

    /**
     * 图片上传
     */
    public function upload_img()
    {
        $priv  = array($this->_pvprefix . "_al");
        $model = $this->model;
        $auth  = $this->model->checkPriv($priv);
        if ($auth) {
            $data = $model->upImg($_FILES['pic']);
            echo json_encode($data);die;
        }

    }

}
